package com.group.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.group.entity.LoginUser;
import com.group.entity.user.UserAccountEntity;
import com.group.exception.IndustryException;
import com.group.mapper.UserAccountMapper;
import com.group.mapper.UserHasAuthorityMapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private UserAccountMapper userAccountMapper;
    @Resource
    private UserHasAuthorityMapper userHasAuthorityMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserAccountEntity entity = this.userAccountMapper.selectOne(new QueryWrapper<UserAccountEntity>().eq("user_account", username));
        if (entity!=null){
            //查找用户权限信息
            List<String> userHasAuthorityList = this.userHasAuthorityMapper.findUserHasAuthorityList(entity.getId());
            //查找用户角色信息
            System.out.println("查找了用户权限数据"+userHasAuthorityList);
            return new LoginUser(entity,userHasAuthorityList);
        }
        throw new IndustryException("账号名或者密码错误");
    }
}
